package org.compression.services.imp;

import java.util.List;

import org.compression.mapper.AuthGroupMapper;
import org.compression.mapper.BussineMapper;
import org.compression.mapper.GroupMapper;
import org.compression.mapper.MagazineMapper;
import org.compression.mapper.MemberMapper;
import org.compression.mapper.UserGroupMapper;
import org.compression.mapper.UserMapper;
import org.compression.services.UserService;
import org.compression.util.RedisUtil;
import org.model.Member;
import org.model.auth.AuthGroup;
import org.model.auth.Group;
import org.model.auth.PageUser;
import org.model.auth.User;
import org.model.auth.UserGroup;
import org.model.comm.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class UserServiceImp implements UserService {

	@Autowired
	UserMapper mMemberMapper;

	@Autowired
	GroupMapper mGroupMapper;


	@Autowired
	RedisUtil	mRedisUtil;
	
	@Autowired
	MagazineMapper mMagazineMapper ;

	@Autowired
	AuthGroupMapper mAuthGroupMapper ;
	
	
	
	@Autowired
	UserGroupMapper mUserGroupMapper;
	
	
	
	@Autowired 
	BussineMapper mBussineMapper;
	
	
	
	
	
	
	
	

	@Override
	public Object getMember(Page mPage) {
		PageUser m=new PageUser();
		Object date=(Object) mRedisUtil.get(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize());
		if(date!=null) {
			return date;
		}else {
			m.setMember(mMemberMapper.getMembers(mPage.getCurrentPageNum(),mPage.getPageSize()));
			m.setTotal(mMemberMapper.getTotal());
			mRedisUtil.set(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize(), m);
		}
		return m;
	}

	//gc 回收
	@Override
	public Object getGroups(Page mPage) {
		PageUser m=new PageUser();
		Object date=(Object) mRedisUtil.get(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[1].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize());
		if(date!=null) {
			return date;
		}else {
			m.setMember(mGroupMapper.getGroups(mPage.getCurrentPageNum(),mPage.getPageSize()));
			m.setTotal(mGroupMapper.getTotal());
			mRedisUtil.set(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[1].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize(), m);
		}
		return m;
	}
	
	
	//新增更新缓存
	@Override
	public Object getMagazinePage(Page mPage) {
		PageUser m=new PageUser();
		long total=mMagazineMapper.getTotal();
		Object date=(Object) mRedisUtil.get(this.getClass().getName()+this.getClass().getDeclaredMethods()[2].getName()+0+total);
		if(date!=null) {
			return date;
		}else {
			m.setMember(mMagazineMapper.getMagazinePage(0,total));
			m.setTotal(total);
			mRedisUtil.set(this.getClass().getName()+this.getClass().getDeclaredMethods()[2].getName()+0+total, m);
		}
		return m;
	}



	//用户拥有的权限
	@Override
    public List<AuthGroup> getGroupRelate(String id){
		return	mAuthGroupMapper.getGroupRelate(id);
	}

	
	
	//刷新缓存
	public Object insertGroupMagazineId(AuthGroup mAuthGroup) {
		if(mAuthGroupMapper.getselectById(mAuthGroup.getBookId(), mAuthGroup.getGroupId())==null) {
			mAuthGroupMapper.insert(mAuthGroup);
			return getGroupRelate(mAuthGroup.getGroupId()+"");
		}
		return "0000";
	}
	
	
	
	//更新缓存
	@Override
	public Object deleteRelate(AuthGroup mAuthGroup) {
		  mAuthGroupMapper.deleteRelate(mAuthGroup.getBookId(), mAuthGroup.getGroupId());
		  return getGroupRelate(mAuthGroup.getGroupId()+"");
	}
	
	@Override
	public Object getMembersGroup(String id) {
		return mUserGroupMapper.getMembersGroup(id);
	}
	


	@Override
	public Object	deleteUserRelate(UserGroup mUserGroup) {
		 mUserGroupMapper.deleteUserRelate(mUserGroup.getMemberId(),mUserGroup.getGroupId());
		 if(mUserGroup.getGroupId()==3) {
			  return mBussineMapper.getBussinetableDataRealte();
		 }
		 return mUserGroupMapper.getMembersGroup(mUserGroup.getGroupId()+""); 
	}
	
	
	@Override
	public Object	insertSelective(UserGroup mUserGroup) {
		if(mUserGroupMapper.getselectById(mUserGroup.getMemberId(), mUserGroup.getGroupId())==null) {
			 mUserGroupMapper.insertSelective(mUserGroup);
		}
		if(mUserGroup.getGroupId()==3) {
			 return mBussineMapper.getBussinetableDataRealte();
		}
		return mUserGroupMapper.getMembersGroup(mUserGroup.getGroupId()+"");
		
	}

	@Override
	public void add(User Member) {

		
	}
	@Override
	public Object getPageBussiness(Page mPage) {
		PageUser m=new PageUser();
		m.setTotal(mBussineMapper.getTotal());
		m.setMember(mBussineMapper.getPageBussiness(mPage));
		return	m;
	}
	
	@Override
	public Object getBussinetableDataRealte() {
		return mBussineMapper.getBussinetableDataRealte();
	}
	
	
	
	
	
	
	
	
	

/*	@Override
	public Object deleteRelate(AuthGroup mAuthGroup) {
		mAuthGroupMapper.deleteRelate(mAuthGroup.getBookId(), mAuthGroup.getGroupId());
		return null;
	}*/

}
